<!-- 登录日志 -->
<template>
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :has-add="false"
        :has-edit="false"
        :has-save="false"
        :has-del="false"
        :has-cancel="false"
        :has-export="true"
        @cancel="cancel"
        @add="add"
        @del="del"
        @edit="edit"
        @export="goExport"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">登录地址:</div>
          <div class="input">
            <el-input
              placeholder="请输入登录地址"
              v-model="searchForm.ipaddr"
              clearable
              @keyup.enter.native="search"
              @blur="search"
              @clear="search"
            />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">登录名称:</div>
          <div class="input">
            <el-input
              placeholder="请输入登录名称"
              v-model="searchForm.loginName"
              clearable
              @keyup.enter.native="search"
              @blur="search"
              @clear="search"
            />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">登录状态:</div>
          <div class="input">
            <el-select v-model="searchForm.status" placeholder="请选择登录状态" clearable>
              <el-option
                v-for="item in statusList"
                :key="item.status"
                :label="item.title"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">登录时间:</div>
          <div class="input">
            <el-date-picker
              v-model="time"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              @change="getSearchTime"
            >
            </el-date-picker>
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <!-- 业务组件展示表格 -->
      <div class="zh-table-view">
        <el-table
          :data="tableData"
          style="width: 100%"
          @row-click="handleEdit"
          height="100%"
        >
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column prop="infoId" label="访问编号" />
          <el-table-column prop="loginName" label="登录名称" />
          <el-table-column prop="ipaddr" label="登录地址" />
          <el-table-column prop="loginLocation" label="登录地点" />
          <el-table-column prop="browser" label="浏览器" />
          <el-table-column prop="os" label="操作系统" />
          <el-table-column prop="status" label="登录状态">
            <template slot-scope="scope">
              {{ scope.row.status === '0' ? "成功" : "失败" }}
            </template>
          </el-table-column>
          <el-table-column prop="msg" label="操作信息" />
          <el-table-column prop="operTime" label="操作时间">
            <template slot-scope="scope">
              {{ getTime(scope.row.operTime) }}
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="warning"
                @click="lookDetails(scope.row)"
                >详细</el-button
              >
            </template>
          </el-table-column> -->
        </el-table>
      </div>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog> </template>
  </BaseMainSlot>
</template>
  <script>
import axios from "axios";
import baseURL from '@/api/baseURL'
import API from "@/api/log-manage/login-log";
import ActionBar from "@/components/ActionBar/index";
import { mymixin } from "@/utils/mixins";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import Pagination from "@/components/Pagination";
export default {
  components: { ActionBar, BaseMainSlot, Pagination, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      totalSize: 0,
      // ---添加相关---
      formData: {},
      databaseName: "DB_TDW1_PUBLIC",
      searchForm: {
        ipaddr: "",
        loginName: "",
        status: "",
        startTime: "",
        endTime: "",
      },
      businessTypeList: [
        { title: "其它", value: 0 },
        { title: "新增", value: 1 },
        { title: "修改", value: 2 },
        { title: "删除", value: 3 },
        { title: "授权", value: 4 },
        { title: "导出", value: 5 },
        { title: "导入", value: 6 },
        { title: "强退", value: 7 },
        { title: "生成代码", value: 8 },
        { title: "清空数据", value: 9 },
      ],
      statusList: [
        { title: "成功", value: '0' },
        { title: "失败", value: '1' },
      ],
      time: "",
    };
  },
  mounted() {
    this.gettabledataList();
  },
  methods: {
    // 操作栏
    lookDetails() {
      console.log("详细");
    },
    goExport() {
      let params = {
        ...this.searchForm,
      };
      let url = '/sysLogininfor/export'
      let fileName = '登录日志.xls'
      this.downLoadFile(url,params,fileName)
    },
    async downLoadFile(url,params,fileName) {
      const { data: data } = await axios({
        method: "post",
        url:  baseURL.logServiceUrl + url,
        responseType: "blob",
        headers: {
          // accessToken: sessionStorage.token,
          // databaseName: sessionStorage.databaseName,
          // organizationList: sessionStorage.organizationList,
          // organizationNo: sessionStorage.organizationNo,
          // virtualSystemNo: sessionStorage.virtualSystemNo,
        },
        data: params.entity || params,
      });
      const link = document.createElement("a");
      link.download = fileName;
      link.style.display = "none";
      link.href = URL.createObjectURL(data);
      document.body.appendChild(link);
      link.click();
      URL.revokeObjectURL(link.href);
      document.body.removeChild(link);
    },
    // 获取表格信息
    async gettabledataList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        condition: {
          ...this.searchForm,
        },
      };
      let res = await API.getList(params);
      if (res.code === 200) {
        this.tableData = res.data.content;
        this.totalSize = res.data.totalElements;
      } else {
        this.$message.warning("获取表格失败");
      }
    },
    // 搜索
    search() {
      this.currentPage = 1;
      this.gettabledataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.gettabledataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.gettabledataList();
    },
    getTime(timestamp) {
      let date = new Date();
      if (timestamp) date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000

      const Y = date.getFullYear() + "-";
      const M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      const D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";

      const h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      const m =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      const s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    },
    getSearchTime(e) {
      if(e === null) {
        this.$set(this.searchForm, "startTime", '');
        this.$set(this.searchForm, "endTime", '');
      } else {
        this.$set(this.searchForm, "startTime", new Date(e[0]));
        this.$set(this.searchForm, "endTime", new Date(e[1]));
      }
    },
  },
};
</script>
    
  <style lang="scss" scoped>
@import "~@/styles/variables.scss";

.zh-table-view {
  width: 100%;
  height: calc(100% - 40px);
  position: relative;
}
</style>
    